package com.yuyou.fn.common.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import org.springframework.stereotype.Service;

import com.yuyou.fn.common.mapper.ConfigurationTableMapper;
import com.yuyou.fn.common.entity.ConfigurationTable;
import com.yuyou.fn.common.service.IConfigurationTableService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import java.util.*;

/**
 *
 * ConfigurationTable 表数据服务层接口实现类
 *
 */
@Service
public class ConfigurationTableServiceImpl extends SuperServiceImpl<ConfigurationTableMapper, ConfigurationTable,Long> implements IConfigurationTableService {

    @Override
    public HashMap<String, Object> findConfigByCond(ConfigurationTable configurationTable) {
        Example<ConfigurationTable> example=ConfigurationTable.createExample();
        Example.Criteria criteria=example.createCriteria();
        List<String> typeList=new ArrayList<String>();
        if(configurationTable.getType()!=null){
            String types=configurationTable.getType();

            String[] typeArray=types.split(",");
            for(String type:typeArray){
                typeList.add(type);
            }
            criteria.andIn("type",typeList);
        }
        if(configurationTable.getPosition()!=null){
            criteria.andLike("position",configurationTable.getPosition());
        }else{
            configurationTable.setPosition("default");//默认
            criteria.andLike("position",configurationTable.getPosition());
        }
        example.orderBy("type");
        example.orderBy("level");
        example.orderBy("code");
        List<ConfigurationTable> configurationTableList=findListByExample(example);
        HashMap<String,Object> resultMap=new HashMap<String,Object>();
        Iterator<ConfigurationTable> it=configurationTableList.iterator();
        while(it.hasNext()){
            ConfigurationTable configurationTable1=it.next();
            String type=configurationTable1.getType();
            String level=configurationTable1.getLevel();
            if("1".equals(level)) {
                HashMap<String,Object> tempMap=new HashMap<String,Object>();
                tempMap.put("code",configurationTable1.getCode());
                tempMap.put("name",configurationTable1.getName());
                tempMap.put("value",configurationTable1.getValue());
                List<HashMap<String,Object>> subList=null;
                HashMap<String, Object> subMap=null;
                if(resultMap.containsKey(type)) {
                    subMap = (HashMap<String, Object>) resultMap.get(type);
                }else{
                    subMap =new  HashMap<String, Object>();
                }
                if(subMap.containsKey("subList")){
                    subList = (  List<HashMap<String,Object>> )subMap.get("subList");
                }else {
                    subList =new   ArrayList<HashMap<String,Object>>();
                }
                subList.add(tempMap);
                subMap.put("subList",subList);
                resultMap.put(type,subMap);
                it.remove();
            }
        }

       for(Map.Entry<String,Object> entry:resultMap.entrySet()){
            String type=entry.getKey();
            HashMap<String,Object> subMap=(HashMap<String,Object>) entry.getValue();
           List<HashMap<String,Object>> subList=(List<HashMap<String,Object>>)subMap.get("subList");
            for (HashMap<String,Object> tempMap:subList){
                fillMap(type,tempMap,configurationTableList);
            }
       }
       for (String type:typeList){
            if(!resultMap.containsKey(type)){
                //configurationTable.setPosition("default");//默认
                example.clear();
                Example.Criteria criteria1=example.createCriteria();
                criteria1.andLike("position","default");
                criteria1.andEqualTo("type",type);
                List<ConfigurationTable> configurationTableList1=findListByExample(example);
                Iterator<ConfigurationTable> it1=configurationTableList1.iterator();
                while(it1.hasNext()){
                    ConfigurationTable configurationTable1=it1.next();
                    String type1=configurationTable1.getType();
                    String level=configurationTable1.getLevel();
                    if("1".equals(level)) {
                        HashMap<String,Object> tempMap=new HashMap<String,Object>();
                        tempMap.put("code",configurationTable1.getCode());
                        tempMap.put("name",configurationTable1.getName());
                        tempMap.put("value",configurationTable1.getValue());
                        List<HashMap<String,Object>> subList=null;
                        HashMap<String, Object> subMap=null;
                        if(resultMap.containsKey(type1)) {
                            subMap = (HashMap<String, Object>) resultMap.get(type1);
                        }else{
                            subMap =new  HashMap<String, Object>();
                        }
                        if(subMap.containsKey("subList")){
                            subList = (  List<HashMap<String,Object>> )subMap.get("subList");
                        }else {
                            subList =new   ArrayList<HashMap<String,Object>>();
                        }
                        subList.add(tempMap);
                        subMap.put("subList",subList);
                        resultMap.put(type1,subMap);
                        it1.remove();
                    }
                }
                for(Map.Entry<String,Object> entry:resultMap.entrySet()){
                    String type2=entry.getKey();
                    HashMap<String,Object> subMap=(HashMap<String,Object>) entry.getValue();
                    List<HashMap<String,Object>> subList=(List<HashMap<String,Object>>)subMap.get("subList");
                    for (HashMap<String,Object> tempMap:subList){
                        fillMap(type2,tempMap,configurationTableList1);
                    }
                }
            }
       }

        return resultMap;
    }

    @Override
    public Long countConfigByLevelAndNameAndTypeAndPosition(String level, String name, String type, String position) {
        Example<ConfigurationTable> example=ConfigurationTable.createExample();
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("level",level);
        criteria.andEqualTo("name",name);
        criteria.andEqualTo("type",type);
        criteria.andEqualTo("position",position);

        return this.countByExample(example);
    }

    void fillMap(String type, HashMap<String,Object> tempMap, List<ConfigurationTable> configurationTableList){
        Iterator<ConfigurationTable> it=configurationTableList.iterator();
        String code=(String) tempMap.get("code");
        while(it.hasNext()){
            ConfigurationTable configurationTable1=it.next();
            String parentCode=configurationTable1.getParentcode();
            String childType=configurationTable1.getType();
            int hasChild=configurationTable1.getHaschild()==null?0:configurationTable1.getHaschild();
            if(code.equals(parentCode)&&type.equals(childType)){
                List<HashMap<String,Object>> subList=null;
                if(tempMap.containsKey("subList")){
                    subList=(List<HashMap<String,Object>>)tempMap.get("subList");
                }else{
                    subList=new ArrayList<HashMap<String,Object>>();
                }
                HashMap<String,Object> subMap=new HashMap<String,Object>();
                subMap.put("code",configurationTable1.getCode());
                subMap.put("name",configurationTable1.getName());
                subMap.put("value",configurationTable1.getValue());
                subList.add(subMap);
                tempMap.put("subList",subList);
                it.remove();
                if (hasChild==1)fillMap(childType,subMap,configurationTableList);
            }

        }

    }
}